﻿using System;
using System.Data;
using System.Reflection;
using System.Windows.Forms;

namespace QL_Truong_Hoc.Export
{
    public partial class frmExportExcel : DevExpress.XtraEditors.XtraForm
    {
        private const string mcstrRoleCode = "bao_cao";

        public DevExpress.XtraSpreadsheet.SpreadsheetControl XlsControl { get { return this.XlsBaoCao; } }

        private const string mcstrExportFail = "Tạo báo cáo thất bại. Xin hãy thử lại.";

        public frmExportExcel()
        {
            InitializeComponent();
        }
        
        private void frmExportExcel_Load(object sender, EventArgs e)
        {
            if (!clsCommon.fncCheckRole(Program.glstRoles, mcstrRoleCode))
            {
                this.BeginInvoke(new MethodInvoker(this.Close));
                return;
            }
        }

        /// <summary>
        /// Xuất danh sách lớp nghề
        /// </summary>
        /// <param name="intLopID"></param>
        /// <returns></returns>
        public bool fncExportDanhSachLopNghe(int intLopID)
        {
            bool blnResult = false;
            DataTable objData = null;

            try
            {
                if (!clsCommon.fncCheckRole(Program.glstRoles, mcstrRoleCode, false))
                {
                    return false;
                }

                clsDSLopNghe objExport;

                objExport = new clsDSLopNghe();
                objData = DAO.clsDbAccess.fncGetDanhSachLopNghe(Program.gobjDb, intLopID);

                if (!objExport.fncExport(this.XlsBaoCao, objData, true))
                {
                    clsCommon.fncMsgError(mcstrExportFail);
                    return false;
                }

                this.Text = "Danh sách lớp nghề";
                return true;
            }
            catch (Exception ex)
            { clsCommon.fncMsgError(MethodBase.GetCurrentMethod().ReflectedType.Name, MethodBase.GetCurrentMethod().Name, ex.Message); }
            finally
            { if (objData != null) objData.Dispose(); }
            return blnResult;
        }

        /// <summary>
        /// Xuất danh sách lớp văn hóa
        /// </summary>
        /// <param name="intLopID"></param>
        /// <returns></returns>
        public bool fncExportDanhSachLopVanHoa(int intLopID)
        {
            bool blnResult = false;
            DataTable objData = null;

            try
            {
                if (!clsCommon.fncCheckRole(Program.glstRoles, mcstrRoleCode))
                {
                    return false;
                }

                clsDSLopVanHoa objExport;

                objExport = new clsDSLopVanHoa();
                objData = DAO.clsDbAccess.fncGetDanhSachLopVanHoa(Program.gobjDb, intLopID);

                if (!objExport.fncExport(this.XlsBaoCao, objData, true))
                {
                    clsCommon.fncMsgError(mcstrExportFail);
                    return false;
                }

                this.Text = "Danh sách lớp văn hóa";
                return true;
            }
            catch (Exception ex)
            { clsCommon.fncMsgError(MethodBase.GetCurrentMethod().ReflectedType.Name, MethodBase.GetCurrentMethod().Name, ex.Message); }
            finally
            { if (objData != null) objData.Dispose(); }
            return blnResult;
        }

        /// <summary>
        /// Xuất điểm môn văn hóa
        /// </summary>
        /// <param name="intLopID"></param>
        /// <param name="intMonID"></param>
        /// <returns></returns>
        public bool fncExportDiemMonVanHoa(int intLopID, int intMonID)
        {
            bool blnResult = false;
            DataTable objData = null;

            try
            {
                if (!clsCommon.fncCheckRole(Program.glstRoles, mcstrRoleCode))
                {
                    return false;
                }

                clsDiemVanHoa objExport;

                objExport = new clsDiemVanHoa();
                objData = DAO.clsDbAccess.fncGetDiemVanHoa(Program.gobjDb, intLopID, intMonID);

                if (!objExport.fncExport(this.XlsBaoCao, objData, (clsEnum.MON_VAN_HOA)intMonID, true))
                {
                    clsCommon.fncMsgError(mcstrExportFail);
                    return false;
                }

                switch (intMonID)
                {
                    case (int)clsEnum.MON_VAN_HOA.TOAN:
                        this.Text = "Danh sách điểm môn Toán";
                        break;

                    case (int)clsEnum.MON_VAN_HOA.LY:
                        this.Text = "Danh sách điểm môn Lý";
                        break;

                    case (int)clsEnum.MON_VAN_HOA.HOA:
                        this.Text = "Danh sách điểm môn Hóa";
                        break;

                    case (int)clsEnum.MON_VAN_HOA.VAN:
                        this.Text = "Danh sách điểm môn Văn";
                        break;
                }

                return true;
            }
            catch (Exception ex)
            { clsCommon.fncMsgError(MethodBase.GetCurrentMethod().ReflectedType.Name, MethodBase.GetCurrentMethod().Name, ex.Message); }
            finally
            { if (objData != null) objData.Dispose(); }
            return blnResult;
        }

        /// <summary>
        /// Xuất điểm môn văn hóa 4 môn tổng hợp
        /// </summary>
        /// <param name="intLopID"></param>
        /// <param name="intMonID"></param>
        /// <returns></returns>
        public bool fncExportDiemMonVanHoa4Mon(int intLopID)
        {
            bool blnResult = false;
            DataTable objData = null;

            try
            {
                if (!clsCommon.fncCheckRole(Program.glstRoles, mcstrRoleCode))
                {
                    return false;
                }

                clsDiemVanHoa4Mon objExport;

                objExport = new clsDiemVanHoa4Mon();

                //xuất điểm toán
                objData = DAO.clsDbAccess.fncGetDiemVanHoa(Program.gobjDb, intLopID, (int)clsEnum.MON_VAN_HOA.TOAN);
                if (!objExport.fncExport(this.XlsBaoCao, objData, clsEnum.MON_VAN_HOA.TOAN, true))
                {
                    clsCommon.fncMsgError(mcstrExportFail);
                    return false;
                }

                //xuất điểm lý
                objData = DAO.clsDbAccess.fncGetDiemVanHoa(Program.gobjDb, intLopID, (int)clsEnum.MON_VAN_HOA.LY);
                if (!objExport.fncExport(this.XlsBaoCao, objData, clsEnum.MON_VAN_HOA.LY, true))
                {
                    clsCommon.fncMsgError(mcstrExportFail);
                    return false;
                }

                //xuất điểm hóa
                objData = DAO.clsDbAccess.fncGetDiemVanHoa(Program.gobjDb, intLopID, (int)clsEnum.MON_VAN_HOA.HOA);
                if (!objExport.fncExport(this.XlsBaoCao, objData, clsEnum.MON_VAN_HOA.HOA, true))
                {
                    clsCommon.fncMsgError(mcstrExportFail);
                    return false;
                }

                //xuất điểm văn
                objData = DAO.clsDbAccess.fncGetDiemVanHoa(Program.gobjDb, intLopID, (int)clsEnum.MON_VAN_HOA.VAN);
                if (!objExport.fncExport(this.XlsBaoCao, objData, clsEnum.MON_VAN_HOA.VAN, true))
                {
                    clsCommon.fncMsgError(mcstrExportFail);
                    return false;
                }


                this.Text = "Danh sách điểm tổng hợp 4 môn văn hóa";

                return true;
            }
            catch (Exception ex)
            { clsCommon.fncMsgError(MethodBase.GetCurrentMethod().ReflectedType.Name, MethodBase.GetCurrentMethod().Name, ex.Message); }
            finally
            { if (objData != null) objData.Dispose(); }
            return blnResult;
        }

        /// <summary>
        /// Xuất điểm môn nghề
        /// </summary>
        /// <param name="intLopID"></param>
        /// <param name="intMonID"></param>
        /// <returns></returns>
        public bool fncExportDiemMonNghe(int intLopID, int intMonID)
        {
            bool blnResult = false;
            DataTable objData = null;

            try
            {
                if (!clsCommon.fncCheckRole(Program.glstRoles, mcstrRoleCode))
                {
                    return false;
                }

                clsDiemNghe objExport;

                objExport = new clsDiemNghe();
                objData = DAO.clsDbAccess.fncGetDiemNghe(Program.gobjDb, intLopID, intMonID);

                if (!objExport.fncExport(this.XlsBaoCao, objData, true))
                {
                    clsCommon.fncMsgError(mcstrExportFail);
                    return false;
                }

                return true;
            }
            catch (Exception ex)
            { clsCommon.fncMsgError(MethodBase.GetCurrentMethod().ReflectedType.Name, MethodBase.GetCurrentMethod().Name, ex.Message); }
            finally
            { if (objData != null) objData.Dispose(); }
            return blnResult;
        }

        /// <summary>
        /// Xuất danh sách thi hết môn nghề
        /// </summary>
        /// <param name="stOption"></param>
        /// <param name="objData"></param>
        /// <returns></returns>
        public bool fncExportDanhSachThiHetMonNghe(clsDSKiemTraKetThucMonNghe.stSetting stOption, DataTable objData)
        {
            bool blnResult = false;

            try
            {
                if (!clsCommon.fncCheckRole(Program.glstRoles, mcstrRoleCode))
                {
                    return false;
                }

                clsDSKiemTraKetThucMonNghe objExport;

                objExport = new clsDSKiemTraKetThucMonNghe();

                if (!objExport.fncExport(this.XlsBaoCao, stOption, objData, true))
                {
                    clsCommon.fncMsgError(mcstrExportFail);
                    return false;
                }

                this.Text = string.Format("Danh sách kiểm tra kết thúc môn {0} lớp {1}", stOption.strMon, stOption.strLop);

                return true;
            }
            catch (Exception ex)
            { clsCommon.fncMsgError(MethodBase.GetCurrentMethod().ReflectedType.Name, MethodBase.GetCurrentMethod().Name, ex.Message); }
            finally
            { if (objData != null) objData.Dispose(); }
            return blnResult;
        }

        /// <summary>
        /// Xuất danh sách thi tn nghề
        /// </summary>
        /// <param name="intTTN_ID"></param>
        /// <returns></returns>
        public bool fncExportDanhSachThiTotNghiep(int intTTN_ID, int intTrinhDo)
        {
            bool blnResult = false;
            DataTable objData = null;

            try
            {
                if (!clsCommon.fncCheckRole(Program.glstRoles, mcstrRoleCode))
                {
                    return false;
                }

                clsDSThiTotNghiepSC objExportSC;
                clsDSThiTotNghiepCD objExportCD;

                objExportSC = new clsDSThiTotNghiepSC();
                objExportCD = new clsDSThiTotNghiepCD();

                objData = DAO.clsDbAccess.fncGetDsThiTotNghiepNghe(Program.gobjDb, intTTN_ID);

                if (intTrinhDo == (int)clsEnum.TRINH_DO_DAO_TAO.SO_CAP)
                {
                    if (!objExportSC.fncExport(this.XlsBaoCao, objData, true))
                    {
                        clsCommon.fncMsgError(mcstrExportFail);
                        return false;
                    }
                }
                else
                {
                    if (!objExportCD.fncExport(this.XlsBaoCao, objData, true))
                    {
                        clsCommon.fncMsgError(mcstrExportFail);
                        return false;
                    }
                }

                return true;
            }
            catch (Exception ex)
            { clsCommon.fncMsgError(MethodBase.GetCurrentMethod().ReflectedType.Name, MethodBase.GetCurrentMethod().Name, ex.Message); }
            finally
            { if (objData != null) objData.Dispose(); }
            return blnResult;
        }

        /// <summary>
        /// Xuất danh sách thi tn nghề
        /// </summary>
        /// <param name="intTTN_ID"></param>
        /// <returns></returns>
        public bool fncExportDanhSachThiTotNghiepVanHoa_KetQua(DataTable objData)
        {
            bool blnResult = false;

            try
            {
                if (!clsCommon.fncCheckRole(Program.glstRoles, mcstrRoleCode))
                {
                    return false;
                }

                clsDSThiTotNghiepVanHoa_KetQua objExportVHKQ;
                objExportVHKQ = new clsDSThiTotNghiepVanHoa_KetQua();

                if (!objExportVHKQ.fncExport(this.XlsBaoCao, objData, true))
                {
                    clsCommon.fncMsgError(mcstrExportFail);
                    return false;
                }

                this.Text = "Danh sách kiểm tra kết thúc môn học văn hóa";

                return true;
            }
            catch (Exception ex)
            { clsCommon.fncMsgError(MethodBase.GetCurrentMethod().ReflectedType.Name, MethodBase.GetCurrentMethod().Name, ex.Message); }
            finally
            { if (objData != null) objData.Dispose(); }
            return blnResult;
        }

    }
}